#include <iostream>
#include <algorithm>
using namespace std;

#define INF 0x3f3f3f3f

const int N = 1e5 + 5;
int f[N], cnt[N];
int n, ff;

struct People
{
    int id;
    int cnt;
} parr[N];

bool mycmp(const People &p1, const People &p2)
{
    if (p1.cnt != p2.cnt)
    {
        return p1.cnt > p2.cnt;
    }
    return p1.id < p2.id;
}

void merge(int a, int b)
{
    f[a] = b; // 有点像链状的并查集，不更新父节点
}

int init(int i)
{
    if (cnt[i])
    {
        return cnt[i];
    }
    return cnt[i] = max(cnt[i], init(f[i]) + 1);
}

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int input;
        cin >> input;
        if (input == -1)
        {
            ff = i;
        }
        merge(i, input);
    }
    cnt[ff] = 1;
    for (int i = 1; i <= n; i++)
    {
        cnt[i] = init(i);
        parr[i].cnt = cnt[i];
        parr[i].id = i;
    }
    sort(parr + 1, parr + 1 + n, mycmp);
    int maxb = parr[1].cnt;
    cout << maxb << endl;
    int i = 1;
    int flag = 0;
    while (parr[i].cnt == maxb)
    {
        if (flag)
        {
            cout << " ";
        }
        cout << parr[i].id;
        flag = 1;
        i++;
    }
    return 0;
}